import { Component, OnInit, Injector, TemplateRef, ViewChild, Input } from '@angular/core';
import { NgZorroPageBaseComponent } from '../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component';
import { HttpService } from "../../core/service/http.service";
import { HttpClient } from "@angular/common/http";
import { Router, NavigationExtras } from "@angular/router";
import {
  AndOr, Compare, ExpressionPair, PagedResultDto, PagerSearchInput, ElementViewType, BaseComponentConfig,
  SystemFormControl, FieldType, ElementShowType, CustomNavigation, ClassInfoOutput, NavigatType, ArSystemFormControl, ClassSchool
} from "../..";
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { CityListComponent } from '../school/cityList';
import {
  FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, Validators,
  FormControl
} from "@angular/forms";



@Component({
  selector: 'app-studentmanagexe',
  templateUrl: './studentmanagexe.component.html',
  styleUrls: ['../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component.scss']
})
export class StudentmanagexeComponent extends NgZorroPageBaseComponent implements OnInit {
  cityData;
  provinceData;
  selectedProvince;
  selectedCity;
  schoolName;
  collegelName;
  manalName;
  resultxy;
  resultxymana;
  listEntitiesdata;
  aclear;
  classmana;
  delesh = [];
  disabledButton = true;
  displayData: Array<{ name: string; age: number; address: string; checked: boolean }> = [];
  dataSet = [];
  checkedNumber = 0;
  operating = false;
  smisVisible = false; // 删除显示
  isVisiblemm = false; // 密码显示
  repassword; // 密码
  pid;

  ngOnInit() {
    this.IniObserver.subscribe(sb => {
      this.getEntityList();
    });
    // this.listSearchValue = [
    //   new ExpressionPair('id', "", Compare.Equal, AndOr.And),
    //   new ExpressionPair('className', "", Compare.Equal, AndOr.And),
    // ];
  }
  constructor(
    injecter: Injector,
    public http1: HttpService,
    private modalService: NzModalService,
    public http: HttpClient

  ) {
    super(injecter,
      new BaseComponentConfig(
        "StudentInfo/GetAllStudentInfo",
        "",
        "",
        "",
        "",
        "id",
        "",
        "",
        [],
        "",
        { Edit: false, Dell: false },
      ));

    const city = new CityListComponent;
    this.cityData = city.cityDatas;
    this.provinceData = city.provinceDatas;


  }
  // 根据省市获取学校
  async citygetschool(e) {
    const ss = this.selectedProvince + "-" + this.selectedCity;
    this.listEntitiesdata = await this.http1.get<any>('School/GetSchoolByAddress?address=' + ss);
  }
  // 根据学校获取学院
  async provinceChangesol(e) {
    this.resultxy = await this.http1.get<any>('School/GetAllAcademeBySchool?input=' + e);
    this.collegelName = "";
  }
  // 根据学院获取系
  async getmana(e) {
    this.resultxymana = await this.http1.get<any>('School/GetDepartmentByAcademe?aId=' + e);

  }

  // 学院搜索
  async searchChange(searchText: string) {
    const that = this;
    clearTimeout(this.aclear);
    this.aclear = setTimeout(() => {
      that.sealist(searchText);
    }, 1000);
  }
  sealist(i) {
    const datalist = { "Name": i };
    this.listEntitiesdata = [];
    this.http1.post<any>("School/GetSchoolToTen", datalist).then(e => {
      this.listEntitiesdata = e;

    });
  }

  getNewEntity(): ClassSchool {
    return new ClassSchool();
  }


  async navigateCustom(url: string, pid: any) {
    const msgId = this.message.loading('正在获取数据', { nzDuration: 0 }).messageId;
    try {
      const findEntity = await this.httpService.get<ClassSchool>(`${this.componentConfig.GetSingle}?input=${pid}`);
      if (findEntity) {
        const navigationExtras: NavigationExtras = {
          queryParams: { 'id': pid, 'caption': findEntity.schoolName }
        };
        this.router.navigate([url], navigationExtras);
      } else {
        this.message.error('无法确定表单信息！请刷新列表！');
      }
    }
    finally {
      this.message.remove(msgId);
    }
  }
  // 判断添加还是编辑跳转
  editor(id: string) {
    this.router.navigate(['app/studentindex/studentadd'], {
      queryParams: {
        studentId: id
      }
    });
  }
  classadd(id: string) {
    this.router.navigate(['app/studentindex/studentadd'], {
      queryParams: {
        studentId: id
      }
    });
  }
  // 删除
  async delestudent() {
    this.checkedNumber = this.listEntities.entities.filter(value => value.checked).length;
    this.smisVisible = true;

  }
  async handleOksm(type: string) {
    this.smisVisible = false;
    await this.http1.post<any>('StudentInfo/DeleteStudentInfo', this.delesh);
    this.delesh = [];
    this.getEntityList();
    this.message.create(type, `删除成功`);

  }
  handleCancel() {
    this.smisVisible = false;
    this.isVisiblemm = false;
  }
  showModalForComponent(entity: ClassSchool, tag: any) {

  }
  // 搜索
  classsearch() {
    this.getEntityList();
  }
  // 搜索取消
  classcancle() {
    this.listSearchValue = [
      new ExpressionPair('id', "", Compare.Equal, AndOr.And),
      new ExpressionPair('className', "", Compare.Equal, AndOr.And),
    ];
  }
  // 勾选判断
  refreshStatus1(id, e): void {
    if (e === true) {
      this.delesh.push(id);
    } else {
      for (let i = 0; i < this.delesh.length; i++) {
        if (id === this.delesh[i]) {
          this.delesh.splice(i, 1);
        }
      }
    }
    const allChecked = this.displayData.every(value => value.checked === true);
    const allUnChecked = this.displayData.every(value => !value.checked);
    this.allChecked = allChecked;
    this.indeterminate = (!allChecked) && (!allUnChecked);
    this.disabledButton = !this.dataSet.some(value => value.checked);
    this.checkedNumber = this.dataSet.filter(value => value.checked).length;
  }
  // 全选判断
  checkAll(value: boolean): void {
    this.delesh = [];
    this.displayData.forEach(data => data.checked = value);
    // tslint:disable-next-line:no-shadowed-variable
    const allChecked = this.displayData.every(value => value.checked === true);
    // tslint:disable-next-line:no-shadowed-variable
    const allUnChecked = this.displayData.every(value => !value.checked);
    this.indeterminate = (!allChecked) && (!allUnChecked);
    // tslint:disable-next-line:no-shadowed-variable
    this.disabledButton = !this.dataSet.some(value => value.checked);
    // tslint:disable-next-line:no-shadowed-variable
    this.checkedNumber = this.dataSet.filter(value => value.checked).length;
    if (value === true) {
      for (let i = 0; i < this.listEntities.entities.length; i++) {
        this.delesh.push(this.listEntities.entities[i].id);
      }
    } else {
      this.delesh = [];
    }
  }
  operateData(): void {
    this.operating = true;
    setTimeout(_ => {
      this.dataSet.forEach(value => value.checked = false);
      this.refreshStatus1("2", "1");
      this.operating = false;
    }, 1000);
  }
  currentPageDataChange($event: Array<{ name: string; age: number; address: string; checked: boolean }>): void {
    this.displayData = $event;
  }
  classsearchinput(e) {
    if (e.keyCode === 13) {
      this.classsearch();
    }
  }
  // 搜索条件
  classsch(e) {
    this.listSearchValue = [
      new ExpressionPair('address', "", Compare.Contains, AndOr.And),
      new ExpressionPair('schoolId', "", Compare.Contains, AndOr.And),
      new ExpressionPair('academeId', "", Compare.Contains, AndOr.And),
      new ExpressionPair('collegeDepartmentId', "", Compare.Contains, AndOr.And),
      new ExpressionPair('collegeDepartmentName', "", Compare.Contains, AndOr.And),
    ];
    if (e === "fal") {
      this.listSearchValue[0].propertyValue = "";
      this.listSearchValue[1].propertyValue = "";
      this.listSearchValue[2].propertyValue = "";
      this.listSearchValue[3].propertyValue = "";
      this.listSearchValue[4].propertyValue = "";
    } else {
      let address = e[0];
      let schoolId = e[1];
      let academeId = e[2];
      let collegeDepartmentId = e[3];
      let collegeDepartmentName = e[4];
      if (address === undefined + "-" + undefined) {
        address = "";
      }

      if (schoolId === undefined) {
        schoolId = "";
      }
      if (academeId === undefined) {
        academeId = "";
      }
      if (collegeDepartmentId === undefined) {
        collegeDepartmentId = "";
      }
      if (collegeDepartmentName === undefined) {
        collegeDepartmentName = "";
      }
      this.listSearchValue[0].propertyValue = address;
      this.listSearchValue[1].propertyValue = schoolId;
      this.listSearchValue[2].propertyValue = academeId;
      this.listSearchValue[3].propertyValue = collegeDepartmentId;
      this.listSearchValue[4].propertyValue = collegeDepartmentName;
    }

    this.getEntityList();

    // this.listSearchValue = [

    // 	new ExpressionPair('collegeDepartmentName', "", Compare.Contains, AndOr.And),
    // 	// new ExpressionPair('remarks', "", Compare.Contains, AndOr.And),
    // 	new ExpressionPair('Cid', "", Compare.Equal, AndOr.And),
    //   ];

    // 	this.listSearchValue[0].propertyValue = name;
    // 	this.listSearchValue[0].compare = 0;
    // 	// this.listSearchValue[1].propertyValue = name;
    // 	// this.listSearchValue[1].compare = 0;
    // 	this.listSearchValue[1].propertyValue = xname;
    // 	this.listSearchValue[1].compare = 0;
    //   this.getEntityList();
  }
  // 重置密码
  ngpassword(id) {
    this.pid = id;
    this.http1.get<any>(`SchoolAcademeManager/GetPwd?input=${id}`).then(e => {
      this.repassword = e;
    });

    this.isVisiblemm = true;
  }
  async handleOkmm(type: string) {
    if (this.repassword === undefined) {
      this.message.info('密码不能为空');
    } else {
      this.isVisiblemm = false;
      const data = { id: this.pid, userPwd: this.repassword };
      await this.http1.post<any>('SchoolAcademeManager/UpdatePassword', data);
      this.message.create(type, `重置成功`);
    }
  }
}
